﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;

namespace TestPictrueGenerator
{
    public class Pixel
    {
        public int x { set; get; }
        public int y { set; get; }
        public int color { set; get; }

        public Pixel(int x, int y, int color)
        {
            this.x = x;
            this.y = y;
            this.color = color;
        }

        public static int C2I(Color c)
        {
            return c.R * 65536 + c.G * 256 + c.B;
        }

        public static Color I2C(int c)
        {
            return Color.FromArgb(c / 65536, c % 65536 / 256, c % 256);
        }
    }
    

    class BitmapGenerator
    {
        public int resX { set; get; }
        public int resY { set; get; }

        public BitmapGenerator(int x, int y)
        {
            this.resX = x;
            this.resY = y;
        }

        public Bitmap createFrameImage(int width)
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布
            sourcegra.Clear(Color.White);

            SolidBrush b = new SolidBrush(Color.Black);
            Rectangle r = new Rectangle(width, width, resX - 2 * width, resY - 2 * width);
            sourcegra.FillRectangle(b, r);

            return bitmap;
        }

        public Bitmap createFrameCircleImage(int width)
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);
            sourcegra.Clear(Color.White);

            SolidBrush b = new SolidBrush(Color.Black);
            Rectangle r = new Rectangle(width, width, resX - 2 * width, resY - 2 * width);
            sourcegra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;

            sourcegra.FillEllipse(b, r);

            /*Bitmap bitmapf = (Bitmap)bitmap.Clone();
            bitmapf.RotateFlip(RotateFlipType.Rotate180FlipNone);

            bitmap

            bitmap = bitmap + bitmapf;*/

            return bitmap;
        }

        public Bitmap createColorBar()
        {
            Bitmap imagedata = new Bitmap(resX, resY);

            var sourcegra = Graphics.FromImage(imagedata);//存入画布
            sourcegra.Clear(Color.White);

            Point ps = new Point(0, 0);
            Point pm = new Point(resX / 2, 0);
            Point pe = new Point(resX, 0);

            Color cs = Color.Black;
            Color ce = Color.FromArgb(255, 0, 0);

            LinearGradientBrush lgb;

            // red
            cs = Color.Black;
            ce = Color.FromArgb(255, 0, 0);
            lgb = new LinearGradientBrush(ps, pe, cs, ce);
            sourcegra.FillRectangle(lgb, 0, 0, resX, resY / 5);

            // green
            cs = Color.Black;
            ce = Color.FromArgb(0, 255, 0);
            lgb = new LinearGradientBrush(ps, pe, cs, ce);
            sourcegra.FillRectangle(lgb, 0, resY / 5, resX, 2 * resY / 5);

            // blue
            cs = Color.Black;
            ce = Color.FromArgb(0, 0, 255);
            lgb = new LinearGradientBrush(ps, pe, cs, ce);
            sourcegra.FillRectangle(lgb, 0, 2 * resY / 5, resX, 3 * resY / 5);

            // white
            cs = Color.Black;
            ce = Color.FromArgb(255, 255, 255);
            lgb = new LinearGradientBrush(ps, pe, cs, ce);
            sourcegra.FillRectangle(lgb, 0, 3 * resY / 5, resX, 4 * resY / 5);

            cs = Color.FromArgb(0, 255, 255);
            Color cm = Color.FromArgb(255, 0, 255);
            ce = Color.FromArgb(255, 255, 0);

            cs = Color.FromArgb(0, 255, 255);
            ce = Color.FromArgb(255, 0, 255);
            lgb = new LinearGradientBrush(ps, new Point(resX / 2, 0), cs, ce);
            sourcegra.FillRectangle(lgb, 0, 4 * resY / 5, resX / 2, resY / 5 + 1);

            cs = Color.FromArgb(255, 0, 255);
            ce = Color.FromArgb(255, 255, 0);
            lgb = new LinearGradientBrush(new Point(resX / 2 - 1, 0), pe, cs, ce);
            sourcegra.FillRectangle(lgb, resX / 2, 4 * resY / 5, resX / 2, resY / 5 + 1);

            return imagedata;
        }

        public Bitmap createColorImage(Color c)
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布

            sourcegra.Clear(c);
            return bitmap;
        }

        public Bitmap createScaleImage()
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            Pen whitePen = new Pen(Color.White, 1);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布

            for (int x = 0; x < bitmap.Width; x++)
            {
                if (x % 2 == 0)
                {
                    sourcegra.DrawLine(whitePen, x, 0, x, 2);
                } else
                {
                    sourcegra.DrawLine(whitePen, x, 3, x, 5);
                }

                if (x % 10 == 0)
                {
                    sourcegra.DrawLine(whitePen, x, 0, x, 10);
                }

                if (x % 50 == 0)
                {
                    sourcegra.DrawLine(whitePen, x, 0, x, 15);
                }

                if (x % 100 == 0)
                {
                    sourcegra.DrawLine(whitePen, x, 0, x, 20);
                    if (x != 0)
                        sourcegra.DrawString(x.ToString(), new Font("宋体", 16, FontStyle.Regular), new SolidBrush(Color.White), x, 30);
                }
            }

            for (int y = 0; y < bitmap.Height; y++)
            {
                if (y % 2 == 0)
                {
                    sourcegra.DrawLine(whitePen, 0, y, 2, y);
                }
                else
                {
                    sourcegra.DrawLine(whitePen, 3, y, 5, y);
                }

                if (y % 10 == 0)
                {
                    sourcegra.DrawLine(whitePen, 0, y, 10, y);
                }

                if(y % 50 == 0)
                {
                    sourcegra.DrawLine(whitePen, 0, y, 15, y);
                }

                if (y % 100 == 0)
                {
                    sourcegra.DrawLine(whitePen, 0, y, 20, y);
                    if (y != 0)
                        sourcegra.DrawString(y.ToString(), new Font("宋体", 16, FontStyle.Regular), new SolidBrush(Color.White), 30, y);
                }
            }

            return bitmap;
        }

        public Bitmap createFrameTriangleImage()
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布

            sourcegra.Clear(Color.White);

            Pen whitePen = new Pen(Color.Black, 1);
            sourcegra.DrawLine(whitePen, 0, 0, bitmap.Width, 0);
            sourcegra.DrawLine(whitePen, 0, 0, 0, bitmap.Height);
            sourcegra.DrawLine(whitePen, 0, bitmap.Height - 1, bitmap.Width, bitmap.Height - 1);
            sourcegra.DrawLine(whitePen, bitmap.Width - 1, 0, bitmap.Width - 1, bitmap.Height);

            Image myImage = Properties.Resources.triangle;
            TextureBrush myTextureBrush = new TextureBrush(myImage);
            sourcegra.FillRectangle(myTextureBrush, 8, 8, resX-16, resY - 16);

            return bitmap;
        }

        public Bitmap createChessImage(int gs)
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布
            sourcegra.Clear(Color.White);

            Bitmap texture = new Bitmap(2 * gs, 2 * gs);
            var gt = Graphics.FromImage(texture);
            gt.Clear(Color.White);

            using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
            {
                gt.FillRectangle(myBrush, new Rectangle(0, 0, gs, gs));
                gt.FillRectangle(myBrush, new Rectangle(gs, gs, gs, gs));
            }

            TextureBrush myTextureBrush = new TextureBrush(texture);

            sourcegra.FillRectangle(myTextureBrush, 0, 0, resX, resY);

            return bitmap;
        }

        public Bitmap create_image_four_quadrant_square_texture(List<Color> ca,int square_width)
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布

            Bitmap texture = new Bitmap(2 * square_width, 2 * square_width);
            var gt = Graphics.FromImage(texture);
            gt.Clear(Color.White);


            TextureBrush myTextureBrush = new TextureBrush(texture);


            SolidBrush b_lt = new SolidBrush(ca[0]);
            SolidBrush b_rt = new SolidBrush(ca[1]);
            SolidBrush b_ld = new SolidBrush(ca[2]);
            SolidBrush b_rd = new SolidBrush(ca[3]);

            gt.FillRectangle(b_lt, new Rectangle(0, 0, square_width, square_width));
            gt.FillRectangle(b_rt, new Rectangle(square_width, 0, square_width, square_width));
            gt.FillRectangle(b_ld, new Rectangle(0, square_width, square_width, square_width));
            gt.FillRectangle(b_rd, new Rectangle(square_width, square_width, square_width, square_width));

            myTextureBrush = new TextureBrush(texture);

            sourcegra.FillRectangle(myTextureBrush, 0, 0, resX, resY);

            return bitmap;
        }

        public Bitmap createFlickerPattern(Color co, Color ce, int th)
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布
            //sourcegra.Clear(Color.White);


            Bitmap texture = new Bitmap(2, 2*th);
            var gt = Graphics.FromImage(texture);
            gt.Clear(co);
            Pen pt = new Pen(ce, 1);
            gt.DrawLine(pt, 0, 0-1, 0, th-1);
            gt.DrawLine(pt, 1, th, 1, 2*th);

            
            TextureBrush myTextureBrush = new TextureBrush(texture);

            sourcegra.FillRectangle(myTextureBrush, 0, 0, resX , resY);

            return bitmap;
        }

        public Bitmap createFlickerHLine(Color co, Color ce, int th)
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布
            //sourcegra.Clear(Color.White);


            Bitmap texture = new Bitmap(resX, 2);
            var gt = Graphics.FromImage(texture);
            gt.Clear(co);
            Pen pt = new Pen(ce, 1);
            gt.DrawLine(pt, 0,1, resX, 1);
            //gt.DrawLine(pt, 1, th, 1, 2 * th);


            TextureBrush myTextureBrush = new TextureBrush(texture);

            sourcegra.FillRectangle(myTextureBrush, 0, 0, resX, resY);

            return bitmap;
        }

        public Bitmap createFlickerVLine(Color co, Color ce, int th)
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布
            //sourcegra.Clear(Color.White);


            Bitmap texture = new Bitmap(2, resY);
            var gt = Graphics.FromImage(texture);
            gt.Clear(co);
            Pen pt = new Pen(ce, 1);
            gt.DrawLine(pt, 1, 0, 1, resY);
            //gt.DrawLine(pt, 1, th, 1, 2 * th);


            TextureBrush myTextureBrush = new TextureBrush(texture);

            sourcegra.FillRectangle(myTextureBrush, 0, 0, resX, resY);

            return bitmap;
        }

        public Bitmap createFlickerImage()
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            Pen whitePen = new Pen(Color.White, 1);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布

            for (int x = 0; x < bitmap.Width; x += 100)
            {
                sourcegra.DrawLine(whitePen, x, 0, x, 20);
                if (x != 0)
                    sourcegra.DrawString(x.ToString(), new Font("宋体", 16, FontStyle.Regular), new SolidBrush(Color.White), x, 30);
            }

            for (int x = 0; x < bitmap.Width; x += 10)
            {
                sourcegra.DrawLine(whitePen, x, 0, x, 10);
            }

            for (int y = 0; y < bitmap.Height; y += 100)
            {
                sourcegra.DrawLine(whitePen, 0, y, 20, y);
                if (y != 0)
                    sourcegra.DrawString(y.ToString(), new Font("宋体", 16, FontStyle.Regular), new SolidBrush(Color.White), 30, y);
            }

            for (int y = 0; y < bitmap.Height; y += 10)
            {
                sourcegra.DrawLine(whitePen, 0, y, 10, y);
            }

            return bitmap;
        }

        public Bitmap createChessImage()
        {
            Bitmap bitmap = new Bitmap(resX, resY);
            Pen whitePen = new Pen(Color.White, 1);
            var sourcegra = Graphics.FromImage(bitmap);//存入画布

            for (int x = 0; x < bitmap.Width; x += 100)
            {
                sourcegra.DrawLine(whitePen, x, 0, x, 20);
                if (x != 0)
                    sourcegra.DrawString(x.ToString(), new Font("宋体", 16, FontStyle.Regular), new SolidBrush(Color.White), x, 30);
            }

            for (int x = 0; x < bitmap.Width; x += 10)
            {
                sourcegra.DrawLine(whitePen, x, 0, x, 10);
            }

            for (int y = 0; y < bitmap.Height; y += 100)
            {
                sourcegra.DrawLine(whitePen, 0, y, 20, y);
                if (y != 0)
                    sourcegra.DrawString(y.ToString(), new Font("宋体", 16, FontStyle.Regular), new SolidBrush(Color.White), 30, y);
            }

            for (int y = 0; y < bitmap.Height; y += 10)
            {
                sourcegra.DrawLine(whitePen, 0, y, 10, y);
            }

            return bitmap;
        }
    }
}
